﻿using System.Linq;
using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;
using RayDen.MegaRay.Core;
using RayDen.RayEngine.Core.Types;

namespace RayDen.MegaRay.GPU
{
    public class RayBuffer : RayBufferBase<RayRecord, RayHit> 
    {
        public bool traced;

        static RayBuffer()
        {
            RayBufferSize = 1048576;
        }

        public int HitCount
        {
            get { return this.rayHits.Count(item => item.Index != RayBuffer.NotHit); }
        }

        public int NonZeroHitCount
        {
            get
            {
                return this.rayHits.Count(item => item.Index != RayBuffer.NotHit & item.Index > 0);
            }
        }

        public RayBuffer(int size) : base(size) { }

        public override bool IsHit(int index)
        {
            return this.rayHits[index].Index != NotHit;
        }

        public override void Reset()
        {
            base.Reset();
            traced = false;
        }

        public bool IsBlocked(int index, int eyeRayIndex)
        {
            var rayHit = this.rayHits[index].Index != NotHit;
            var samePrim = this.rayHits[index].Index == this.rayHits[eyeRayIndex].Index;
            return rayHit && !samePrim;
        }

        public void GetHitPoint(int index, out Point result)
        {
            result = this.RaysInfo[index].Point(this.rayHits[index].Distance);
        }

    }
}